/*
 * @Author: your name
 * @Date: 2024-04-09 13:42:56
 * @LastEditTime: 2024-04-09 13:52:38
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\20.有效的括号.cpp
 */
/*
 * @lc app=leetcode.cn id=20 lang=cpp
 *
 * [20] 有效的括号
 */

// @lc code=start
class Solution {
public:
    bool isValid(string s) {
        if(s.size()%2 != 0){
            // 奇数个
            return false;
        }
        stack<char> st;
        for(char ss:s){
            if(!st.empty()){
                switch (ss)
                {
                case '}':
                    if(st.top() == '{'){
                        st.pop();
                    }else{
                        st.push(ss);
                    }
                    break;
                case ')':
                    if(st.top() == '('){
                        st.pop();
                    }else{
                        st.push(ss);
                    }
                    break;
                case ']':
                    if(st.top() == '['){
                        st.pop();
                    }else{
                        st.push(ss);
                    }
                    break;
                default:
                    st.push(ss);
                    break;
                }
            }else{
                st.push(ss);
            }
        }
        return st.empty();
    }
};
// @lc code=end

